package com.dut.bbs.activity;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.dut.bbs.R;
import com.dut.bbs.adapter.Top10Adapter;
import com.dut.bbs.adapter.Top10Adapter.ViewHolder;
import com.dut.bbs.data.ParseTop10;
import com.dut.bbs.util.Dg;
import com.dut.bbs.util.URLConstant;
import com.dut.bbs.web.HtmlSource;

public class Top10Activity extends BaseActivity {
	private ListView top10list;
	private List<Map<String,String>> ItemList;
	private Context context;
	private Handler handler = new Handler(){
		@Override  
        public void handleMessage(Message msg) {
			if(msg.what==1){
				Top10Adapter adapter = new Top10Adapter(context,ItemList);
				top10list.setAdapter(adapter);
			}else{
				Dg.showToast(context, "抓取页面失败！");
			}		
			getProgressDialog().dismiss();
		}
	};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.topten);
		context = Top10Activity.this;
		top10list = (ListView) findViewById(R.id.toptenlist);
		top10list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ViewHolder viewHolder = (ViewHolder) view.getTag();
				if (viewHolder.url.length() > 0) {
					Intent intent = new Intent(Top10Activity.this, ThreadActivity.class);
					intent.putExtra("url", viewHolder.url);
					intent.putExtra("title", viewHolder.tv_title.getText());
					startActivity(intent);
				}
			}
		});
		getProgressDialog().setMessage("抓取页面中...");
		getProgressDialog().show();
        Runnable r = new ConnectThread();
		new Thread(r).start();
	}
	class ConnectThread implements Runnable {
		int message =0;
		@Override
		public void run(){
			try {
				String data = HtmlSource.getHtml_data(URLConstant.TOP10_URI);
				ItemList = ParseTop10.get10BigNews(data);
				message = 1;
				handler.sendEmptyMessage(message);
			} catch (Exception e) {
				Log.v("Top10Activity", e.toString());
				handler.sendEmptyMessage(message);
			}
		}
		
	}
}
